package com.dharma.mydiet.storage;

import java.util.ArrayList;
import java.util.List;

import com.dharma.mydiet.MainApplication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DAORegisterPersonal {

	public final static String KEY_ID = "idRegistroPersonal";
	public final static String KEY_ID_REGISTER = "idRegister";
	public final static String KEY_ID_USER = "idUser";
	public final static String KEY_DATE = "date";
	public final static String KEY_QTY = "quantity";
	public final static String DATABASE_TABLE = "registerPersonal";
	SQLiteDatabase myDataBase;

	private static final String[] cols = new String[] { KEY_ID, KEY_ID_REGISTER, KEY_ID_USER, KEY_DATE, KEY_QTY };

	
	private static DAORegisterPersonal instance;

	public static DAORegisterPersonal getInstance (){
		if (instance==null){
			instance=new DAORegisterPersonal();
		}
		return instance;
	}
	
	public DAORegisterPersonal() {
		myDataBase = DBManager.getInstance().getMyDataBase();
	}

	public void insertFood(TransactionalRegisterPersonal registerFood) {
		ContentValues newValues = new ContentValues();
		//newValues.put(KEY_ID, registerFood.getIdRegisterPersonal());
		newValues.put(KEY_ID_REGISTER, registerFood.getIdRegister());
		newValues.put(KEY_ID_USER, registerFood.getIdUser());
		newValues.put(KEY_DATE, registerFood.getDateFormattedString());
		newValues.put(KEY_QTY, registerFood.getQuantity());
		myDataBase.beginTransaction();
		myDataBase.insert(DATABASE_TABLE, null, newValues);
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
	}

	public boolean removeRegisterFood(TransactionalRegisterPersonal registerFood) {

		boolean ret = false;
		int row = this.getRow(String.valueOf(registerFood.getIdRegisterPersonal()));
		if (row >= 0) {
			ret = removeRegisterFood(row);
		}
		return ret;
	}

	private int getRow(String registerFoodId) {
		int row = -1;
		try {
			Cursor result = null;
			result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ID + "='" + registerFoodId + "'", null, null,
					null, null, null);

			if (!((result.getCount() == 0) || !result.moveToFirst())) {
				if (result.moveToFirst()) {
					row = result.getInt(result.getColumnIndex(KEY_ID));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	public boolean removeRegisterFood(long _rowIndex) {
		myDataBase.beginTransaction();
		boolean result = myDataBase.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
		return result;

	}

	public boolean updateRegisterFood(TransactionalRegisterPersonal registerFood) {
		int row = this.getRow(String.valueOf(registerFood.getIdUser()));
		return updateUser(row, registerFood);
	}

	public boolean updateUser(Integer _rowIndex, TransactionalRegisterPersonal registerFood) {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_ID, registerFood.getIdRegisterPersonal());
		newValues.put(KEY_ID_REGISTER, registerFood.getIdRegister());
		newValues.put(KEY_ID_USER, registerFood.getIdUser());
		newValues.put(KEY_DATE, registerFood.getDateFormattedString());
		newValues.put(KEY_QTY, registerFood.getQuantity());
		return myDataBase.update(DATABASE_TABLE, newValues, KEY_ID + "=" + _rowIndex, null) > 0;
	}

	public TransactionalRegisterPersonal getUser(String foodId) {
		TransactionalRegisterPersonal food = null;
		Cursor result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ID + "= '" + foodId + "'", null, null, null,
				null, null);
		if (!((result.getCount() == 0) || !result.moveToFirst())) {
			if (result.moveToFirst()) {
				food = new TransactionalRegisterPersonal(result.getInt(result.getColumnIndex(KEY_ID)),
						result.getInt(result.getColumnIndex(KEY_ID_REGISTER)), result.getInt(result
								.getColumnIndex(KEY_ID_USER)), result.getString(result.getColumnIndex(KEY_DATE)),
						result.getFloat(result.getColumnIndex(KEY_QTY)));
			}
		}
		return food;
	}

	public ArrayList<TransactionalRegisterPersonal> getUsers() {
		ArrayList<TransactionalRegisterPersonal> devices = new ArrayList<TransactionalRegisterPersonal>();
		Cursor result = myDataBase.query(DATABASE_TABLE, cols, KEY_ID_USER + "= '" + MainApplication.getCurrentUser().getIdUser() + "'", null, null, null, KEY_ID);
		if (result.moveToFirst())
			do {
				devices.add(new TransactionalRegisterPersonal(result.getInt(result.getColumnIndex(KEY_ID)), result
						.getInt(result.getColumnIndex(KEY_ID_REGISTER)), result.getInt(result
						.getColumnIndex(KEY_ID_USER)), result.getString(result.getColumnIndex(KEY_DATE)), result
						.getFloat(result.getColumnIndex(KEY_QTY))));
			} while (result.moveToNext());
		return devices;
	}

	
	public ArrayList<TransactionalRegisterPersonal> getRegistersbyIDRegistro(String idRegistro) {
		ArrayList<TransactionalRegisterPersonal> devices = new ArrayList<TransactionalRegisterPersonal>();
		Cursor result = myDataBase.query(DATABASE_TABLE, cols, KEY_ID_USER + "= '" + MainApplication.getCurrentUser().getIdUser() + "' AND " + KEY_ID_REGISTER + "= '" + idRegistro+ "'", null, null, null, KEY_ID);
		if (result.moveToFirst())
			do {
				devices.add(new TransactionalRegisterPersonal(result.getInt(result.getColumnIndex(KEY_ID)), result
						.getInt(result.getColumnIndex(KEY_ID_REGISTER)), result.getInt(result
						.getColumnIndex(KEY_ID_USER)), result.getString(result.getColumnIndex(KEY_DATE)), result
						.getFloat(result.getColumnIndex(KEY_QTY))));
			} while (result.moveToNext());
		return devices;
	}
	public static void create(TransactionalRegisterPersonal register) {

	}

	public static TransactionalRegisterPersonal read(int idRegister) {
		return null;

	}

	public static void update(TransactionalRegisterPersonal register) {

	}

	public static void delete(int idRegister) {

	}
}
